package com.wangyk.mybatis.main;

import com.wangyk.mybatis.LogUtil;
import com.wangyk.mybatis.pojo.MybatisUser;
import com.wangyk.mybatis.pojo.Score;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.util.List;

public class MybatisMain
{
    public static Logger logger = LogManager.getLogger(MybatisMain.class);

    public static void main(String[] args)
        throws FileNotFoundException
    {
        //初始化log4j2的appname和hostname
        LogUtil.initLogEnv();
        InputStream inputStream = new FileInputStream("src\\main\\resources\\mybatis-config.xml");
        Reader reader = new InputStreamReader(inputStream);
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //selectAllUser(sqlSession);
        //selectAll(sqlSession);
        selectAll(sqlSession, "i");
        selectAllScore(sqlSession);
        selectAllScore(sqlSession);//默认开启一级缓存
        System.out.println(System.currentTimeMillis());
    }

    /**
     * @param sqlSession
     * @return java.util.List<com.wangyk.mybatis.pojo.MybatisUser>
     * @throws
     * @Description 查询所有用户
     * @author wangyk
     * @date 2018/11/9 13:32
     */
    public static List<MybatisUser> selectAllUser(SqlSession sqlSession)
    {
        List<MybatisUser> users = sqlSession.selectList("selectAllUser");
        for (MybatisUser tempUser : users)
        {
            System.out.println(tempUser);
        }
        return users;
    }

    /**
     * @param sqlSession
     * @return java.util.List<com.wangyk.mybatis.pojo.MybatisUser>
     * @throws
     * @Description 查询所有用户
     * @author wangyk
     * @date 2018/11/9 13:33
     */
    public static List<MybatisUser> selectAll(SqlSession sqlSession)
    {
        List<MybatisUser> users_selectAll = sqlSession.selectList("selectAll");
        for (MybatisUser tempUser : users_selectAll)
        {
            System.out.println(tempUser);
        }
        return users_selectAll;
    }

    /**
     * @param sqlSession
     * @param useName
     * @return java.util.List<com.wangyk.mybatis.pojo.MybatisUser>
     * @throws
     * @Description 模糊查询所有用户
     * @author wangyk
     * @date 2018/11/9 13:34
     */
    public static List<MybatisUser> selectAll(SqlSession sqlSession, String useName)
    {
        //模糊查询
        List<MybatisUser> users_selectByUseName = sqlSession.selectList("selectByUseName", "i");
        for (MybatisUser tempUser : users_selectByUseName)
        {
            System.out.println(tempUser);
        }
        return users_selectByUseName;
    }

    public static List<Score> selectAllScore(SqlSession sqlSession)
    {
        List<Score> scores = sqlSession.selectList("selectAllScore");
        for (Score tempScore : scores)
        {
            System.out.println(tempScore);
        }
        return scores;
    }

    public void testLog()
    {
        //共有8个级别，按照从低到高为：All < Trace < Debug < Info < Warn < Error < Fatal < OFF.
        logger.fatal("fatal：输出每个严重的错误事件将会导致应用程序的退出的日志。");
        logger.error("error：输出错误信息日志。");
        logger.warn("warn：输出警告及warn以下级别的日志。");
        logger.info("info：消息在粗粒度级别上突出强调应用程序的运行过程");
        logger.debug("debug：指出细粒度信息事件对调试应用程序是非常有帮助的");
        logger.trace("trace：是追踪，就是程序推进以下，你就可以写个trace输出，所以trace应该会特别多，不过没关系，我们可以设置最低日志级别不让他输出。");
    }
}
